﻿using System;
using System.Web.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;
using Wuqi.Webdiyer;

namespace Hxsoft.AppV1.Admin.Controls
{
	public partial class UserSelector : RepeaterSelector<string, List<Users.UserForManageList>>
	{
		private const string _showSelectedText = "当前显示选定信息，取消此项将显示所有的信息。";
		private const string _notShowSelectedText = "当前显示所有信息，选择此项将显示选定的信息。";


		#region 属性
		protected override AspNetPager Pager
		{
			get { return pager; }
		}

		protected override Repeater DataList
		{
			get { return List; }
		}


		public string ShowSelectedText
		{
			get
			{
				object o = ViewState["ShowSelectedText"];
				return o == null ? _showSelectedText : (string)o;
			}
			set { ViewState["ShowSelectedText"] = value; }
		}

		public string NotShowSelectedText
		{
			get
			{
				object o = ViewState["NotShowSelectedText"];
				return o == null ? _notShowSelectedText : (string)o;
			}
			set { ViewState["NotShowSelectedText"] = value; }
		}

		private LoadedValue<int?> loadedSelectCompanyId;
		public int? SelectCompanyId
		{
			get {
				if (loadedSelectCompanyId.Loader == null)
					loadedSelectCompanyId.Loader = () => (int?)ViewState["SelectCompanyId"];
				return loadedSelectCompanyId.Value;
			}
			set {
				if (loadedSelectCompanyId.Setter == null)
					loadedSelectCompanyId.Setter = v => ViewState["SelectCompanyId"] = v;
				loadedSelectCompanyId.Value = value;
			}
		}

		private LoadedValue<bool?> loadedIsApproved;
		public bool? IsApproved
		{
			get {
				if (loadedIsApproved.Loader == null)
					loadedIsApproved.Loader = () =>
						{
							if (ViewState["IsApproved"] == null) return null;
							return (bool)ViewState["IsApproved"];
						};
				return loadedIsApproved.Value;
			}
			set {
				if (loadedIsApproved.Setter == null)
					loadedIsApproved.Setter = v => ViewState["IsApproved"] = v;
				loadedIsApproved.Value = value;
			}
		}


		#endregion

		#region 函数
		protected override PaginationInfo<List<Users.UserForManageList>> GetData()
		{
			return Users.GetPaginationSelectorList(SelectCompanyId, IsApproved, TbKeyword.Text, CbShowSelected.Checked ? SelectedItems == null ? new string[] { } : SelectedItems.ToArray() : null, pager.PageSize, pager.CurrentPageIndex);
		}

		protected string ItemRealName
		{
			get {
				var userId = (Guid?)Eval("UserId");
				if(!userId.HasValue) return Eval("UserName","");
				if (CompanyUsers.Exists(userId.Value))
					return CompanyUsers.GetUser((string)Eval("UserName", "")).RealName;
				return Eval("UserName", "");
			}
		}

		/// <summary>
		/// 取得 ListItem 的标识符。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns>ListItem 的标识符。</returns>
		protected override string GetListItemType(RepeaterItem item)
		{
			string identifier;
			HiddenField control;

			control = (HiddenField)item.FindControl("Identifier_UserName");
			identifier = control.Value;

			return identifier;
		}
		#endregion

		protected void CbShowSelected_CheckedChanged(object sender, EventArgs e)
		{
			LoadList();
			if (CbShowSelected.Checked)
				CbShowSelected.Text = ShowSelectedText;
			else
				CbShowSelected.Text = NotShowSelectedText;
		}
	}
}